#include "stream_service.h"
#include "gstreamer/stream_gst_rtsp.h"
#include "gstreamer/stream_gst_init.h"
#include "gstreamer/stream_gst_webrtc.h"
#include "gstreamer/stream_gst_webrtc_playback.h"
#include "gstreamer/stream_gst_rtmp.h"

#include "common_event.h"

namespace El {
namespace StreamService {

StreamService &StreamService::GetInstance()
{
    static StreamService instance;
    return instance;
}

bool StreamService::Start()
{
    if (upgrade_handle_ < 0) {
        upgrade_handle_ = El::Common::Event::GetInstance().Subscribe(
            "upgrade", [this](const std::string &eventName, const nlohmann::json &data) {
                EL_INFO("receive upgrade event, stream_service stop, eventName:{} data:{}", eventName, data.dump());
                Stop();
            });
    }

    StreamGst::GetInstance().Start();
    StreamGstWebRTC::GetInstance().Start();
    StreamGstWebRTCPlayback::GetInstance().Start();
    StreamGstRtsp::GetInstance().Start();
    StreamGstRtmp::GetInstance().Start();

    return true;
}

bool StreamService::Stop()
{
    StreamGstRtsp::GetInstance().Stop();
    StreamGstWebRTCPlayback::GetInstance().Stop();
    StreamGstWebRTC::GetInstance().Stop();
    StreamGstRtmp::GetInstance().Stop();
    StreamGst::GetInstance().Stop();
    return true;
}

} // namespace StreamService
} // namespace El
